# -*- coding: utf-8 -*-

import re
from django import template
from django.conf import settings
from django.utils.encoding import force_unicode
from django.utils.safestring import mark_safe

try:
    WIKI_WORD_RE = settings.WIKI_WORD_RE
except AttributeError:
    WIKI_WORD_RE = r'(?:[A-Z]+[a-z]+){2,}'

wikiwordfier = re.compile(r'(?<!!)\b(%s)\b' % WIKI_WORD_RE)
#wikiwordfier = re.compile(r'\b(%s)\b' % WIKI_WORD)

register = template.Library()

@register.filter
def wikiwords(s):
    s = wikiwordfier.sub(r'<a href="../\1/">\1</a>', s)
    return force_unicode(s)
wikiwords.is_safe = True


@register.inclusion_tag('wiki/article_content.html')
def render_content(article, content_attr='content', markup_attr='markup'):
    """ Display an the body of an article, rendered with the right markup.
    """
    return {
        'content': getattr(article, content_attr),
        'markup': getattr(article, markup_attr)
    }
